package demo.practice.stack;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class P225 {

    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        System.out.println(myStack.pop());
        myStack.push(2);
        System.out.println(myStack.top());

        System.out.println(myStack.empty());

    }
}

class MyStack {

    List<Integer> list;
    int size = 0;

    /**
     * Initialize your data structure here.
     */
    public MyStack() {
        list = new ArrayList<>();
    }

    /**
     * Push element x onto stack.
     */
    public void push(int x) {
        if(size==list.size()){
            list.add(x);
            size++;
        }else{
            list.set(size,x);
            size++;
        }
    }

    /**
     * Removes the element on top of the stack and returns that element.
     */
    public int pop() {
        if (empty())
            return -1;
        int top = top();
        size--;
        return top;
    }

    /**
     * Get the top element.
     */
    public int top() {
        return empty() ? -1 : list.get(size - 1);
    }

    /**
     * Returns whether the stack is empty.
     */
    public boolean empty() {
        return size == 0;
    }


}